import React from 'react'
import { WeaUpload, WeaRangePicker, WeaInput, WeaInputNumber, WeaTextarea, WeaSelect, WeaBrowser, WeaCheckbox, WeaDatePicker, WeaTimePicker, WeaDateGroup, WeaSelectGroup, WeaScope, WeaTools } from 'ecCom'
import isEmpty from 'lodash/isEmpty'
const types = WeaTools.types;
const getKey = WeaTools.getKey;
import { inject, observer } from 'mobx-react';
import { toJS } from 'mobx';
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

@inject('meetingDialog')
@observer
class Main extends React.Component {
  render() {
    const { fieldConfig, form } = this.props;
    return this.renderComs(mobx.toJS(fieldConfig), form);
  }
  onChange(key, args) {
    let obj = {
      value: args[0],
    };
    if (args[1]) obj.valueSpan = args[1];
    if (args[2]) obj.valueObj = args[2];
    let res = {}; res[key] = obj;
    this.props.onChange && this.props.onChange(res);
  }
  renderComs(c, form) { //config obj text record index
    let { viewAttr = 2, meetingType = '', canset = false } = this.props;
    const type = typeof (c.conditionType) == 'string' ? c.conditionType.toUpperCase() : c.conditionType;
    let key = getKey(c);
    let oldKey = key;
    let bindObj_0 = ''
    let bindObj_1 = ''
    let bindObj_2 = ''
    let bindObj = ''
    if (key.indexOf('remindBeforeStart') != -1 || key.indexOf('remindBeforeEnd') != -1) {
      key = key.split('__');
      bindObj_0 = form.$(key[0])
      bindObj_1 = form.$(key[1])
      bindObj_2 = form.$(key[2])
    } else {
      bindObj = form.$(key);
    }
    delete c.value;
    if (type === types.INPUT) {
      return <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@eg68t5`}
        {...c}
        {...bindObj.bind() }
        onChange={(...args) => { this.onChange(key, args); bindObj.onChange(...args) }}
        value={bindObj.value}
        {...c.otherParams}
      />
    }
    if (typeof (type) == 'object' || type === types.CHECKBOX) {
      return (
        <div className="wea-field-readonly">
          <WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@60fbvw`}
            className={`checkbox_${viewAttr}`}

            {...bindObj_0.bind() }
            onChange={(...args) => { this.onChange(key[0], args); bindObj_0.onChange(...args) }}
            {...c}
            {...c.otherParams}
          />
          <span style={{ padding: '0 8px' }}>{key[0] == 'remindBeforeStart' ? getLabel(19784,'开始前') : getLabel(19785,'结束前')}</span>
          <div style={{ width: '50px' }} className="wea-input-normal" >
          <WeaInputNumber ecId={`${this && this.props && this.props.ecId || ''}_WeaInputNumber@lwqhvw`}
            min="0"
            {...c}
            {...bindObj_1.bind() }
            onChange={(...args) => { this.onChange(key[1], args); bindObj_1.onChange(...args) }}
            value={bindObj_1.value}
            {...c.otherParams}
          />
          </div>
          <span style={{ padding: '0 8px' }}>{getLabel(391,'小时')}</span>
          <div style={{ width: '50px' }} className="wea-input-normal" >
          <WeaInputNumber ecId={`${this && this.props && this.props.ecId || ''}_WeaInputNumber@h5wzzg`}
            min="0"
            {...c}
            {...bindObj_2.bind() }
            onChange={(...args) => { this.onChange(key[2], args); bindObj_2.onChange(...args) }}
            value={bindObj_2.value}
            {...c.otherParams}
          />
          </div>
          <span style={{ padding: '0 8px' }}>{getLabel(15049,'分钟')}</span>
        </div>
      )
    }
    if (type === 'ATTACHEMENT') {
      if (isEmpty(c.cfg)) {
        if (meetingType == 'create') {
          return (
            <div style={{ display: 'inline-block' }}>
              <span style={{ color: '#FF0000' }}>{getLabel(20476,'附件存放目录未设置！')}</span>
              {canset ? <span style={{ marginLeft: '20px' }}><a href="/meeting/Maint/MeetingSet.jsp" target="_blank">{getLabel(384781,'点此设置')}</a></span> : ''}
            </div>)
        } else {
          return (<span />)
        }
      } else {
        return <WeaUpload ecId={`${this && this.props && this.props.ecId || ''}_WeaUpload@er48uv`}
          maxUploadSize={c.cfg.maxUploadSize}//最大上传大小限制，单位为MB
          mixUploadSize={c.cfg.mixUploadSize}//最小上传大小限制，单位为MB
          btnSize="default"//按钮大小
          uploadUrl={`${c.cfg.uploadUrl}?category=${c.cfg.category}`}//文件上传服务器接口地址
          category={`${c.cfg.category}`}//文件上传目录
          onUploading={() => { }}//文件上传状态接收函数
          datas={c.attValue}
          onChange={(ids, list) => {
            let { dialogStatus, getMeetingTopicField, getMeetingServiceField, saveFields } = this.props.meetingDialog;
            let { allCreateDatas } = toJS(dialogStatus);
            //c.domkey[0] 为该参数的key
            let allCreateDatasCopy = { ...allCreateDatas };
            allCreateDatasCopy[c.domkey[0]] = ids.join(',')
            saveFields(allCreateDatasCopy)
          }}
          ref={'newmeetingupload'}
          uploadId={"_newmeetingupload"}//上传组件标识，同时生成隐藏域 id
          style={{ display: "inline-block", padding: "0" }}
          autoUpload={true}//自动上传
          viewAttr={viewAttr}
        />
      }
    }
  }
}

export default Main
